package cn.bjit.service.impl;

import cn.bjit.constant.RedisKey;
import cn.bjit.controller.request.AccountLoginRequest;
import cn.bjit.controller.request.AccountRegisterRequest;
import cn.bjit.enums.AutoTypeEnum;
import cn.bjit.enums.BizCodeEnum;
import cn.bjit.enums.SendCodeEnum;
import cn.bjit.manager.AccountManager;
import cn.bjit.model.AccountDO;
import cn.bjit.model.LoginUser;
import cn.bjit.service.AccountService;
import cn.bjit.service.NotifyService;
import cn.bjit.util.CommonUtil;
import cn.bjit.util.IDUtil;
import cn.bjit.util.JWTUtil;
import cn.bjit.util.JsonData;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.shardingsphere.core.strategy.keygen.SnowflakeShardingKeyGenerator;
import org.mockito.internal.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AccountServiceImpl implements AccountService {
    @Autowired
    private NotifyService notifyService;
    @Autowired
    private AccountManager accountManager;
    /**
     * 注册
     * 1，手机验证码
     * 2，密码加密
     * 3，手机号码唯一性验证
     * 4，插入数据
     * 5，发放福利
     * @param registerRequest
     * @return
     */

    @Override
    public JsonData register(AccountRegisterRequest registerRequest) {
        boolean checkCode=false;
        //1，手机验证码
        if(StringUtils.isNotBlank(registerRequest.getPhone())){
            checkCode=notifyService.checkCode(SendCodeEnum.USER_REGISTER,registerRequest.getPhone(),registerRequest.getCode());
        }
        if(!checkCode){
            return JsonData.buildResult(BizCodeEnum.CODE_ERROR);
        }
        //把accountDO 考给registerRequest
        AccountDO accountDO=new AccountDO();
        BeanUtils.copyProperties(registerRequest,accountDO);
        accountDO.setAuth(AutoTypeEnum.DEFAULT.name());

        //模拟一个唯一账号 后面改
        accountDO.setAccountNo(Long.parseLong(IDUtil.geneSnowFlakeID().toString()));


        //设置密码-加盐
        accountDO.setSecret("$1$"+CommonUtil.getStringNumRandom(8));
        String cryptPwd=Md5Crypt.md5Crypt(registerRequest.getPwd().getBytes(),accountDO.getSecret());
        accountDO.setPwd(cryptPwd);

        int row=accountManager.insert(accountDO);
        log.info("row={},注册成功，{}",row,accountDO);
        //发放福利 TODO
        userRegisterInitTask(accountDO);
        return JsonData.buildSuccess();
    }

    /**
     * 登录
     * 1，根据手机号码去数据库查
     * 2，密码加盐后和数据库中的对比
     * @param accountLoginRequest
     * @return
     */
    @Override
    public JsonData login(AccountLoginRequest accountLoginRequest) {
       List<AccountDO> accountDOList= accountManager.findByPhone(accountLoginRequest.getPhone());
       if(accountDOList!=null&&accountDOList.size()==1){ //查到了
          AccountDO accountDO= accountDOList.get(0); //根据手机号码查到的这个数据就是一个AccountDO对象
          String md5Crypt= Md5Crypt.md5Crypt(accountLoginRequest.getPwd().getBytes(),accountDO.getSecret());
          if(md5Crypt.equalsIgnoreCase(accountDO.getPwd())){
              //生成TOKEN
              LoginUser loginUser=new LoginUser();
              BeanUtils.copyProperties(accountDO,loginUser); //前面的考到后面
              String token= JWTUtil.geneJsonToken(loginUser);
              return JsonData.buildSuccess(token);
          }else{
              return JsonData.buildResult(BizCodeEnum.ACCOUNT_PWD_ERROR);
          }
       }else{//没查到
           return JsonData.buildResult(BizCodeEnum.ACCOUNT_PWD_ERROR);
       }
    }
    /**
     * 发放福利
     * @param accountDO
     */
    private void userRegisterInitTask(AccountDO accountDO) {
    }
}
